/**
 * 适配 tree数据源
 * @author xiufu.wang
 */
export default function applyAdapt(dataSource) {
    dataSource.treeAsyncTreeload = function (treeNode, cb) {
        const opt = dataSource.mergeOption
        const queryFiled = opt.singleQueryField || 'query'
        const childrenField = opt.childrenField || 'children'
        /**
         * 解决业务场景:
         * 按需加载,一次可能加载多级
         */
        if (!treeNode.isLeaf &&  treeNode.data && treeNode.data[childrenField] && treeNode.data[childrenField].length > 0) { 
            cb(treeNode.data[childrenField])
            return
        }
        const res = dataSource.shortLoadDatas({ [queryFiled]: treeNode.level === 0 ? '' : treeNode.key }, true)
        if (res.then) {
            res.then((data) => {
                cb(dataSource.handleFormatDatas(data, opt) || [])
            })
        } else {
            cb(dataSource.handleFormatDatas(res, opt) || [])
        }
    }
}


